package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.*;
import com.ruoyi.baseData.domain.ProceduresEquipmentConfig;
import com.ruoyi.baseData.domain.Process;
import com.ruoyi.baseData.domain.dto.ProceduresEquipmentConfigDto;
import com.ruoyi.baseData.domain.dto.WoProceduresDto;
import com.ruoyi.baseData.domain.vo.*;
import com.ruoyi.baseData.domain.vo.ProceduresEquipmentConfigVo;
import com.ruoyi.baseData.mapper.*;
import com.ruoyi.baseData.service.ProceduresEquipmentConfigService;
import com.ruoyi.baseData.mapper.ProceduresEquipmentConfigMapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.quality.domain.InspectionItem;
import com.ruoyi.quality.domain.Sampling;
import com.ruoyi.quality.domain.vo.InspectionItemVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class ProceduresEquipmentConfigServiceImpl implements ProceduresEquipmentConfigService{


    @Resource
    ProcessRelMapper processRelMapper;


    @Resource
    ProcessMapper processMapper;

    @Resource
    ProceduresEquipmentConfigMapper proceduresEquipmentConfigMapper;

    @Resource
    EquipmentConfigMapper equipmentConfigMapper;

    /**
     *
     */
    @Resource
    ProceduresTeamMapper proceduresTeamMapper;

    @Resource
    TeamMapper teamMapper;



    @Override
    public Page<ProceduresEquipmentConfigVo> getProceduresEquipmentConfigByPage(ProceduresEquipmentConfigDto pageEntity) {
        MPJLambdaWrapper<ProceduresEquipmentConfig> wrapper = new MPJLambdaWrapper<ProceduresEquipmentConfig>()
                .selectAll(ProceduresEquipmentConfig.class)//查询user表全部字段
                .selectAs( Procedures::getName, ProceduresEquipmentConfigVo::getProceduresName )
                .selectAs( EquipmentConfig::getName, ProceduresEquipmentConfigVo::getEquipmentConfigName)
                //记的测试阶段
                .eq(ProceduresEquipmentConfig::getDelFlag, 0)
                .eq(ProceduresEquipmentConfig::getProceduresId, pageEntity.getProceduresId())
                .leftJoin(Procedures.class, Procedures::getId, ProceduresEquipmentConfig::getProceduresId)
                .leftJoin(EquipmentConfig.class, EquipmentConfig::getId, ProceduresEquipmentConfig::getEquipmentConfigId)
                .orderByDesc(ProceduresEquipmentConfig::getCreateTime);

        wrapper = QueryHelp.getPredicateEx(wrapper, ProceduresEquipmentConfig.class, pageEntity);
        Page<ProceduresEquipmentConfigVo> userList = proceduresEquipmentConfigMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   ProceduresEquipmentConfigVo.class, wrapper);
        return userList;
    }

    @Override
    public void addProceduresEquipmentConfigs(List<ProceduresEquipmentConfigDto> proceduresEquipmentConfigDtos) {
        String userName = SecurityUtils.getUsername();
        for(ProceduresEquipmentConfigDto proceduresEquipmentConfigDto : proceduresEquipmentConfigDtos) {
            ProceduresEquipmentConfig proceduresEquipmentConfig = new ProceduresEquipmentConfig();
            BeanUtils.copyProperties(proceduresEquipmentConfigDto, proceduresEquipmentConfig, new String[]{"id"});
            proceduresEquipmentConfig.setDelFlag(false);
            proceduresEquipmentConfig.setCreateBy(userName);
            proceduresEquipmentConfig.setCreateTime(new Date());
            proceduresEquipmentConfigMapper.insert(proceduresEquipmentConfig);
        }
    }

    @Override
    public WoProceduresVo getEquipAndProceduresByWo(WoProceduresDto woProceduresDto) {

        if( woProceduresDto.getSequence() == 0) {
            throw new GlobalException("该工单已经完成");
        }

        WoProceduresVo woProceduresVo = new WoProceduresVo();
        ProcessRelVo processRelVo = new ProcessRelVo();
        List<EquipmentConfigVo> equipmentConfigVos = new ArrayList<>();
        String searchValue2 = woProceduresDto.getWoType() + "工艺";
        MPJLambdaWrapper<Process> wrapper1 = new MPJLambdaWrapper<Process>()
                .eq(Process::getName,searchValue2)
                .eq(Process::getDelFlag, 0);
        Process process = processMapper.selectOne(wrapper1);
        ProcessRel  processRel = new ProcessRel();
        if(null != process) {
            MPJLambdaWrapper<ProcessRel> wapper1 = new MPJLambdaWrapper<ProcessRel>()
                    .eq(ProcessRel::getProcessId,process.getId())
                    .eq(ProcessRel::getSequence, woProceduresDto.getSequence())
                    .eq(Process::getDelFlag, false);
            processRel = processRelMapper.selectOne(wapper1);
            if(null != processRel) {
                BeanUtils.copyProperties(processRel, processRelVo);
            }
        }
        Long proceduresId = processRel.getProceduresId();

        /**
         * 根据工序获取设备
         */
        MPJLambdaWrapper<ProceduresEquipmentConfig> wrapper = new MPJLambdaWrapper<ProceduresEquipmentConfig>()
                .selectAll(ProceduresEquipmentConfig.class)//查询user表全部字段
                .eq(ProceduresEquipmentConfig::getProceduresId, proceduresId)
                //记的测试阶段
                .eq(ProceduresEquipmentConfig::getDelFlag, 0)
                .orderByDesc(ProceduresEquipmentConfig::getCreateTime);
        List<ProceduresEquipmentConfig> proceduresEquipmentConfigs = proceduresEquipmentConfigMapper.selectList(wrapper);

        List<Integer> equipmentConfigIds = proceduresEquipmentConfigs.stream().map(item -> item.getEquipmentConfigId()).collect(Collectors.toList());

        if(null != equipmentConfigIds && equipmentConfigIds.size() > 0) {
            MPJLambdaWrapper<EquipmentConfig> wrapper2 = new MPJLambdaWrapper<EquipmentConfig>()
                    .selectAll(EquipmentConfig.class)//查询user表全部字段
                    .in(EquipmentConfig::getId, equipmentConfigIds)
                    //记的测试阶段
                    .eq(EquipmentConfig::getDelFlag, 0);

            equipmentConfigVos = equipmentConfigMapper.selectJoinList(EquipmentConfigVo.class ,wrapper2);
        }


        /**
         * 根据工序获取班次
         */

        MPJLambdaWrapper<ProceduresTeam> wrapper2 = new MPJLambdaWrapper<ProceduresTeam>()
                .selectAll(ProceduresTeam.class)//查询user表全部字段
                .eq(ProceduresTeam::getProceduresId, proceduresId)
                //记的测试阶段
                .eq(ProceduresTeam::getDelFlag, 0)
                .orderByDesc(ProceduresTeam::getCreateTime);
        List<ProceduresTeam> proceduresTeams = proceduresTeamMapper.selectList(wrapper2);
        List<Long> teamsIds = proceduresTeams.stream().map(item -> item.getTeamId()).collect(Collectors.toList());
        List<TeamVo> TeamVoList = new ArrayList<>();
        if(null != proceduresTeams && proceduresTeams.size() > 0) {
            MPJLambdaWrapper<Team> wrapper3 = new MPJLambdaWrapper<Team>()
                    .selectAll(Team.class)//查询user表全部字段
                    .in(Team::getId, teamsIds)
                    //记的测试阶段
                    .eq(Team::getDelFlag, 0);

            TeamVoList = teamMapper.selectJoinList(TeamVo.class ,wrapper3);
        }

//        for(EquipmentTree equipmentTree :equipmentTrees) {
//            EquipmentConfigVo equipmentConfigVo = new EquipmentConfigVo();
//            equipmentConfigVo.setId(equipmentTree.getLevelId().longValue());
//            equipmentConfigVo.setName(equipmentTree.getName());
//            equipmentConfigVos.add(equipmentConfigVo);
//        }
        woProceduresVo.setTeamVoList(TeamVoList);
        woProceduresVo.setProcessRelVo(processRelVo);
        woProceduresVo.setEquipmentConfigVoList(equipmentConfigVos);
        return woProceduresVo;
    }

    @Override
    public WoProceduresVo getNextEquipAndProceduresByWo(WoProceduresDto woProceduresDto) {
        WoProceduresVo woProceduresVo = new WoProceduresVo();
        ProcessRelVo processRelVo = new ProcessRelVo();
        /**
         * 获取所有工序
         */
        //todo::需要加上物料后续改动
        String searchValue2 = woProceduresDto.getWoType() + "工艺";
        MPJLambdaWrapper<Process> wrapper1 = new MPJLambdaWrapper<Process>()
                .eq(Process::getName,searchValue2)
                .eq(Process::getDelFlag, false);
        Process process = processMapper.selectOne(wrapper1);
        ProcessRel  processRel = new ProcessRel();
        if(null != process) {
            Integer nextSequence = 0;
            if(null != woProceduresDto.getSequence()) {
                nextSequence = woProceduresDto.getSequence() + 1;
            }
            MPJLambdaWrapper<ProcessRel> wapper1 = new MPJLambdaWrapper<ProcessRel>()
                    .eq(ProcessRel::getProcessId,process.getId())
                    .eq(ProcessRel::getSequence, nextSequence)
                    .eq(Process::getDelFlag, false);
            processRel = processRelMapper.selectOne(wapper1);
            if(null != processRel) {
                BeanUtils.copyProperties(processRel, processRelVo);
                woProceduresVo.setProcessRelVo(processRelVo);
            }
        }
        return woProceduresVo;
    }


}




